package com.sas.service.impl;

import com.sas.entity.Employee;
// 移除不存在的枚举导入，统一使用整数类型
import com.sas.repository.EmployeeRepository;
import com.sas.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 员工服务实现类
 */
@Service
@Transactional
public class EmployeeServiceImpl implements EmployeeService {

    private static final Logger log = LoggerFactory.getLogger(EmployeeServiceImpl.class);
    @Autowired
    private EmployeeRepository employeeRepository;

    @Override
    public Employee save(Employee employee) {
        log.info("保存员工信息: {}", employee.getName());
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        if (employee.getStatus() == null) {
            employee.setStatus(1); // 默认状态为在职
        }
        return employeeRepository.save(employee);
    }

    @Override
    @Transactional(readOnly = true)
    public Optional<Employee> findById(Long id) {
        return employeeRepository.findById(id);
    }

    @Override
    @Transactional(readOnly = true)
    public List<Employee> findAll() {
        return employeeRepository.findAll();
    }

    @Override
    @Transactional(readOnly = true)
    public List<Employee> findByStatus(Integer status) {
        return employeeRepository.findByStatus(status);
    }

    @Override
    @Transactional(readOnly = true)
    public List<Employee> findByEmployeeType(Integer employeeType) {
        return employeeRepository.findByEmployeeType(employeeType);
    }

    @Override
    @Transactional(readOnly = true)
    public List<Employee> findByNameContaining(String name) {
        return employeeRepository.findByNameContaining(name);
    }

    @Override
    @Transactional(readOnly = true)
    public List<Employee> findActiveEmployeesOrderByPriority() {
        return employeeRepository.findActiveEmployeesOrderByTypeAndLevel();
    }

    @Override
    public Employee update(Employee employee) {
        log.info("更新员工信息: {}", employee.getName());
        Optional<Employee> existingEmployee = employeeRepository.findById(employee.getId());
        if (existingEmployee.isPresent()) {
            Employee existing = existingEmployee.get();
            // 保留创建时间，更新修改时间
            employee.setCreateTime(existing.getCreateTime());
            employee.setUpdateTime(LocalDateTime.now());
            return employeeRepository.save(employee);
        } else {
            throw new RuntimeException("员工不存在，ID: " + employee.getId());
        }
    }

    @Override
    public void deleteById(Long id) {
        log.info("删除员工，ID: {}", id);
        Optional<Employee> employee = employeeRepository.findById(id);
        if (employee.isPresent()) {
            Employee emp = employee.get();
            emp.setStatus(0); // 软删除，设置状态为0
            emp.setUpdateTime(LocalDateTime.now());
            employeeRepository.save(emp);
        } else {
            throw new RuntimeException("员工不存在，ID: " + id);
        }
    }

    @Override
    public void deleteByIds(List<Long> ids) {
        log.info("批量删除员工，IDs: {}", ids);
        List<Employee> employees = employeeRepository.findAllById(ids);
        employees.forEach(emp -> {
            emp.setStatus(0); // 软删除
            emp.setUpdateTime(LocalDateTime.now());
        });
        employeeRepository.saveAll(employees);
    }

    @Override
    @Transactional(readOnly = true)
    public boolean existsById(Long id) {
        return employeeRepository.existsById(id);
    }

    @Override
    @Transactional(readOnly = true)
    public long count() {
        return employeeRepository.count();
    }

    @Override
    @Transactional(readOnly = true)
    public long countByEmployeeType(Integer employeeType) {
        return employeeRepository.findByEmployeeType(employeeType).size();
    }
}